package com.yeyks.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yeyks.dal.dao.HotelInfoMapper;
import com.yeyks.dal.dao.RoomInfoMapper;
import com.yeyks.dal.domain.HotelInfo;
import com.yeyks.dal.domain.RoomInfo;
import com.yeyks.dto.RoomInfoDetailDTO;
import com.yeyks.exception.ServiceException;
import com.yeyks.service.RoomInfoService;
import com.yeyks.util.ObjectConvertUtil;
import com.yeyks.exception.ServiceException;
import com.yeyks.service.RoomInfoService;
import com.yeyks.util.ObjectConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author fuchangshuai
 * @date 2019/3/7 10:33 AM
 */
@Service
@Slf4j
public class RoomInfoServiceImpl implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private HotelInfoMapper hotelInfoMapper;

    @Override
    public RoomInfo selectByPrimaryKey(Integer roomId) {
        return roomInfoMapper.selectByPrimaryKey(roomId);
    }

    /**
     * 通过房间id获取房间详情
     *
     * @param roomId
     * @return
     */
    @Override
    public RoomInfoDetailDTO roomDetail(Integer roomId) {
        RoomInfo roomInfo = roomInfoMapper.selectByPrimaryKey(roomId);
        if (roomInfo == null) {
            log.info("该房间不存在{}", roomId);
            throw new ServiceException("该房间信息不存在");
        }
        RoomInfoDetailDTO detailDTO = ObjectConvertUtil.convertObject(roomInfo, RoomInfoDetailDTO.class);
        String extraInfo = roomInfo.getExtraInfo();
        JSONObject jsonObject = JSONObject.parseObject(extraInfo);
        List<String> imgUrlList = (List<String>) jsonObject.get("imgUrlList");
        detailDTO.setPicUrls(imgUrlList);
        //通过房间id查询酒店
        HotelInfo hotelInfo = hotelInfoMapper.selectByPrimaryKey(roomInfo.getHotelId());
        if (hotelInfo.getEnabledCoupon() == null) {
            hotelInfo.setEnabledCoupon(1);
        }
        detailDTO.setHotelName(hotelInfo.getName());
        detailDTO.setHotelId(hotelInfo.getId());
        detailDTO.setEnabledCoupon(hotelInfo.getEnabledCoupon());
        return detailDTO;
    }

    /**
     * 通过id进行删除（将isDel字段修改为0）
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id, Integer isDel) {
        return roomInfoMapper.deleteById(id, isDel);
    }
}
